package com.zoo.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zoo.common.Pagination;
import com.zoo.entity.User;
import com.zoo.service.UserBiz;

/**
 * @Email    22582416@qq.com
 * @author   张如利
 * @version  创建时间:2011-6-14下午10:26:00
 */
@Controller
@Scope("prototype")
public class UserAction {
	@Resource
	private UserBiz biz ;
	private List<User> users ;
	private User user ;
	private String message ;
	private Pagination pagination; 
    private Integer pageNo = 1;
	private String url = "user_list.do" ;
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}	
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String list(){
		users = biz.findAll();
		pagination = new Pagination(pageNo,12,users.size(),5, url,"pagenum");
		users = pagination.getPageDate(users);
		return "list" ;
	}
	
	public String login(){
		if(biz.login(user.getLoginName(), user.getPwd())){
			return "main" ;
		}
		return "login" ;
	}
	
	public String loginUI(){
		return "login" ;
	}
	
	public String regUI(){
		return "regUI" ;
	}
	
	public String reg(){
		biz.userReg(user);
		//this.message = "注册成功！" ;
		return list() ;
	}
	
	public String view(){
		user = biz.findByID(user.getId());
		return "view" ;
	}
	
	public String addUI(){
		return "addUI" ;
	}
	
	public String add(){
		biz.addUser(user);
		this.message = "用户添加成功！" ;
		return "success" ;
	}
	
	public String editUI(){
		user = biz.findByID(user.getId());
		return "editUI" ;
	}
	
	public String edit(){
		biz.update(user);
		this.message = "用户修改成功！" ;
		return "success" ;
	}
	
	public String del(){
		biz.delFromID(user.getId());
		this.message = "用户删除成功！" ;
		return "success" ;
	}
	
	public String search(){
		if(user==null){
			user = (User)ActionContext.getContext().getSession().get("condition");
		}
		String loginName = user == null ? "" : user.getLoginName() ;
		users = biz.findAll("loginName like ?",new Object[]{"%"+loginName+"%"});
		pagination = new Pagination(pageNo,12,users.size(),5, "user_search.do","pagenum");
		users = pagination.getPageDate(users);
		ActionContext.getContext().getSession().put("condition", user);
		return "list" ;
	}
}
